package com.woniu.controller;

import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.pojo.League;
import com.woniu.service.ILeagueService;
import com.woniu.vo.LeagueDetailVo;
import com.woniu.vo.LeagueEditVo;
import com.woniu.vo.LeagueQueryVo;
import com.woniu.vo.LeagueSearchVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zbk
 * @since 2022-10-12
 */
@RestController
@RequestMapping("/league")
@Api(tags = "团课管理")
public class LeagueController extends BaseController{

    @Autowired
    private ILeagueService leagueService;

    @ApiOperation("分页搜索")
    @PostMapping("/search/{currentPage}/{pageSize}")
    public Result search(@PathVariable Integer currentPage,
                         @PathVariable Integer pageSize,
                         @RequestBody LeagueQueryVo leagueQueryVo){
        PageResult pageResult = leagueService.search(currentPage, pageSize, leagueQueryVo);
        return toDataResult(pageResult);
    }

    @ApiOperation("增加")
    @PostMapping("/{dynamicTags}")
    public Result add(@RequestBody League league, @PathVariable String[] dynamicTags){
        if(dynamicTags.length > 0){
            String label = "";
            for (String dynamicTag : dynamicTags) {
                label = label + dynamicTag + ",";
            }
            String labels = label.substring(0, label.length() - 1);
            league.setLabel(labels);
        }
        boolean flag = leagueService.save(league);
        return toResult(flag);
    }

    @ApiOperation("修改")
    @PutMapping("/{dynamicTags}")
    public Result edit(@RequestBody League league, @PathVariable String[] dynamicTags){
        if(dynamicTags.length > 0){
            String label = "";
            for (String dynamicTag : dynamicTags) {
                label = label + dynamicTag + ",";
            }
            String labels = label.substring(0, label.length() - 1);
            league.setLabel(labels);
        }
        boolean flag = leagueService.updateById(league);
        return toResult(flag);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable String id){
        boolean flag = leagueService.removeById(id);
        return toResult(flag);
    }

    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    public Result queryById(@PathVariable String id){
        League league = leagueService.getById(id);
        LeagueEditVo leagueEditVo = new LeagueEditVo();
        String label = "";
        label = league.getLabel();
        String[] split = label.split(",");
        leagueEditVo.setLeague(league);
        leagueEditVo.setDynamicTags(split);
        return toDataResult(leagueEditVo);
    }

    @ApiOperation("查询所有课程")
    @GetMapping
    public Result queryAll(){
        List<LeagueSearchVo> leagueList = leagueService.queryAll();
        return toDataResult(leagueList);
    }

    @ApiOperation("团课添加关联会员卡")
    @PostMapping("/relation/{leagueId}")
    public Result addCardRelation(
            @PathVariable String leagueId,
            @RequestBody List<String> cardList){
        leagueService.addCardRelation(leagueId, cardList);
        return success();
    }

}
